<?php
/**
 * [KeCms] Copyright (c) 2017 kecms.cn
 * [Author] King east ( 1207877378@qq.com )
 */

namespace wx;


use ke\Http;

class Message
{
    public static function toArray($content)
    {
        return json_decode(json_encode(simplexml_load_string($content,'SimpleXMLElement',LIBXML_NOCDATA)),true);
    }

    public static function run()
    {
        $data=file_get_contents('php://input');
        if($data){
            $c=self::toArray($data);

            echo 'success';
            exit;
        }

    }


    /**
     * 发送模板消息
     * @param $openid
     * @param $template_id
     * @param $url
     * @param array $data
     * @return array
     */
    public static function sendTemplate($openid,$template_id,$url,array $data)
    {
        $sendData=[
            'touser'=>$openid,
            'template_id'=>$template_id,
            'url'=>$url,
            'data'=>$data
        ];
        $wx=new Auth();
        $r=$wx->accessToken();
        $http=new Http('https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$r,'POST');
        $http->setData(json_encode($sendData));
        if($http->send()){
            $ret=$http->toArray();
            if($ret['errcode']==0){
                return [true,'发送成功'];
            }else{
                return [false,$ret['errmsg']];
            }
        }else{
            return [false,$http->error()];
        }



    }

}